package jwdt.xadmin.bbs.action;

import java.util.List;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.bbs.bean.TBbbs;
import jwdt.xadmin.bbs.bean.TBboard;
import jwdt.xadmin.bbs.bean.TBuser;

import com.ezerg.util.DateUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller
public class BbsAction extends BaseAction {
	
	private String url = "/";
	
	@RequestMapping("/main")
	public Page doNewsAdd(Form f, Module m) throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo==null) {
			f.add("ret", "请登录后再发帖子").add("url", "index.do");
			return m.findPage("tips");
		}
		int boardid = f.getInt("id");
		TBboard board = (TBboard)jdb.get(TBboard.class, boardid);
		if(board==null) {
			board = new TBboard();
			board.setId(0);
		}
		f.add("board", board);
		f.add("boards", jdb.query("select id,name from bbs_board where zt=1"));
		return m.findPage(url.concat("main"));
	}
	
	@RequestMapping("/newsSave")
	public Page doNewsSave(Form f, Module m) throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo==null) {
			f.add("ret", "请登录后再发帖子");
			return m.findPage("tips");
		}
		String username = userInfo.getUsername();
		int boardid = f.getInt("boardid");
		TBboard board = (TBboard)jdb.get(TBboard.class, boardid);
		if(board == null) {
			f.add("ret", "您所选择的栏目不存在");
			return m.findPage("tips");
		}
		if(board.getLockboard()==1 && userInfo.getBbstype()!=1) {
			f.add("ret", "该板块只能由管理人员发帖");
			return m.findPage("tips");
		}
		TBbbs news = (TBbbs)f.copy2Class(TBbbs.class);
		news.setBoardid(boardid);
		news.setUsername(username);
		news.setDateandtime(DateUtil.getDateTime());
		news.setUseremail(userInfo.getUseremail());
		String ret = "发帖成功";
		int id = f.getInt("parentid");
		String url = "detail.do?id="+id;
		if(id == 0) {//新帖子
			url = "boardList.do?id="+boardid;
		} else {
			TBbbs bbs = (TBbbs)jdb.get(TBbbs.class, id);
			news.setTopic(bbs.getTopic());
		}
		try {
			jdb.add(news);
			jdb.execute("update bbs_user set article=article+1 where username=?", new Object[]{username});
		} catch (Exception e) {
			e.printStackTrace();
			ret = "发帖出错";
		}
		f.add("ret", ret).add("url", url);
		return m.findPage("tips");
	}

	@RequestMapping("/newsEdit")
	public Page doNewsEdit(Form f, Module m) throws Exception {
		int id = f.getInt("id");
		TBbbs bbs = (TBbbs)jdb.get(TBbbs.class, id);
		if(bbs == null) {
			f.add("ret", "您所选择的帖子不存在");
			return m.findPage("tips");
		}
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo==null) {
			f.add("ret", "请登录后编辑帖子");
			return m.findPage("tips");
		}
		if(userInfo.getBbstype()==1  || userInfo.getUsername().equals(bbs.getUsername())) {
			TBboard board = (TBboard)jdb.get(TBboard.class, bbs.getBoardid());
			f.add("bbs", bbs).add("board", board);
			f.add("boards", jdb.query("select id,name from bbs_board where zt=1"));
			return m.findPage(url.concat("main"));
		}else{
			f.add("ret", "你无编辑此帖子的权限");
			return m.findPage("tips");
		}
	}

	@RequestMapping("/newsUpdate")
	public Page doNewsUpdate(Form f, Module m) throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo==null) {
			f.add("ret", "请登录后再编辑帖子");
			return m.findPage("tips");
		}
		//String username = userInfo.getUsername();
		int id = f.getInt("id");
		TBbbs bbs = (TBbbs)jdb.get(TBbbs.class, id);
		if(bbs == null) {
			f.add("ret", "您所选择的帖子不存在");
			return m.findPage("tips");
		}
		TBbbs news = (TBbbs)f.copy2Obj(bbs);
		news.setDateandtime(DateUtil.getDateTime());
		news.setUseremail(userInfo.getUseremail());
		String ret = "编辑成功";
		try {
			jdb.update(news);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "编辑出错";
		}
		int parentid = bbs.getParentid();
		String url = "detail.do?id="+id;
		if(parentid != 0) {//删除回复帖子
			url = "detail.do?id="+parentid;
		}
		f.add("ret", ret).add("url", url);
		return m.findPage("tips");
	}

	@RequestMapping("/newsDel")
	public Page doNewsDel(Form f, Module m) throws Exception {
		int id = f.getInt("id");
		TBbbs bbs = (TBbbs)jdb.get(TBbbs.class, id);
		if(bbs == null) {
			f.add("ret", "您所选择的帖子不存在");
			return m.findPage("tips");
		}
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo==null) {
			f.add("ret", "请登录后编辑帖子");
			return m.findPage("tips");
		}
		if(!(userInfo.getUsername().equals(bbs.getUsername()) || userInfo.getBbstype()==1)) {
			f.add("ret", "你无权限删除此帖子");
			return m.findPage("tips");
		}
		int boardid = bbs.getBoardid();
		int parentid = bbs.getParentid();
		String ret = "删除成功";
		try {
			jdb.del(bbs);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "删除出错";
		}
		String url = "boardList.do?id="+boardid;
		if(parentid != 0) {//删除回复帖子
			url = "detail.do?id="+parentid;
		}
		f.add("ret", ret).add("url", url);
		return m.findPage("tips");
	}

	@RequestMapping("/detail")
	public Page doDetail(Form f, Module m) throws Exception {
		int id = f.getInt("id");
		TBbbs bbs = (TBbbs)jdb.get(TBbbs.class, id);
		if(bbs == null) {
			f.add("ret", "您访问的帖子不存在");
			return m.findPage("tips");
		}
		TBboard board = (TBboard)jdb.get(TBboard.class, bbs.getBoardid());
		if(board == null) {
			f.add("ret", "您所选择的栏目不存在");
			return m.findPage("tips");
		}
		try {
			jdb.execute("update bbs_bbs1 set hits=hits+1 where announceid=?", new Object[]{id});
		}catch(Exception e) {
			e.printStackTrace();
		}
		//回复
		@SuppressWarnings("rawtypes")
		List reply =  jdb.query("select * from bbs_bbs1 where  parentid="+bbs.getAnnounceid()+"  and  parentid!=0 and child=0 order by dateandtime ");
		//热门
		f.add("hotlist",jdb.query("SELECT * FROM bbs_bbs1 WHERE isbest=1 and parentID=0 AND locktopic!=1 ORDER BY announceid DESC LIMIT 10"));//热帖
		//
		f.add("bbs",bbs).add("board", board).add("reply", reply);
		
		return m.findPage(url.concat("detail"));
	}
	
}
